package net.xxcl.exam.service;

import java.util.List;

import net.xxcl.exam.mapper.AnswerMapper;
import net.xxcl.exam.mapper.QuestionMapper;
import net.xxcl.exam.mapper.ScoreMapper;
import net.xxcl.exam.model.StuForDelayTime;
import net.xxcl.exam.model.StuForMark;
import net.xxcl.exam.pojo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional()
public class ScoreService {
    @Autowired
    private ScoreMapper scoreMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private AnswerMapper answerMapper;

    private static final Logger logger = LoggerFactory.getLogger(ScoreService.class);

    @Transactional(readOnly = true)
    public int countByExample(Criteria example) {
        int count = this.scoreMapper.countByExample(example);
        logger.debug("count: {}", count);
        return count;
    }

    @Transactional(readOnly = true)
    public Score selectByPrimaryKey(ScoreKey key) {
        return this.scoreMapper.selectByPrimaryKey(key);
    }

    @Transactional(readOnly = true)
    public List<Score> selectByExample(Criteria example) {
        return this.scoreMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(ScoreKey key) {
        return this.scoreMapper.deleteByPrimaryKey(key);
    }

    public int updateByPrimaryKeySelective(Score record) {
        return this.scoreMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Score record) {
        return this.scoreMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(Criteria example) {
        return this.scoreMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Score record, Criteria example) {
        return this.scoreMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Score record, Criteria example) {
        return this.scoreMapper.updateByExample(record, example);
    }

    public int insert(Score record) {
        return this.scoreMapper.insert(record);
    }

    public int insertSelective(Score record) {
        return this.scoreMapper.insertSelective(record);
    }

    /**
     * 获取试卷的所有问题和答案
     * @return
     */
    @Transactional(readOnly = true)
    public List<Question> getPaperObject(int paperId){
        List<Question> questions = questionMapper.selectByPaperId(paperId);
        for(Question question:questions){
            question.setAnswers(answerMapper.selectAnswerByQuestion(question.getId()));
        }
        return questions;
    }

    /**
     * 获取试卷的所有问题和正确答案，用于判分
     * @return
     */
    @Transactional(readOnly = true)
    public List<Question> getPaperRightAnswer(int paperId){
        Criteria criteria = new Criteria();
        List<Question> questions = questionMapper.selectByPaperId(paperId);
        for(Question question:questions){
            criteria.clear();
            criteria.put("isright",1);
            criteria.put("questionid",question.getId());
            question.setAnswers(answerMapper.selectByExample(criteria));
        }
        return questions;
    }

    /**
     * 获取考试延迟界面列表所有数据
     * @return
     */
    @Transactional(readOnly = true)
    public List<StuForDelayTime> getForDelayTime(Criteria example){
        return scoreMapper.getForDelayTime(example);
    }

    /**
     * 获取考试延迟界面列表数据总数
     * @return
     */
    @Transactional(readOnly = true)
    public int countForDelayTime(Criteria example){
        return scoreMapper.countForDelayTime(example);
    }

    @Transactional(readOnly = true)
    public List<Score> selectByPaperId(Integer paperid) {
        return this.scoreMapper.selectByPaperId(paperid);
    }

    /**
     * 获取成绩列表
     * @return
     */
    @Transactional(readOnly = true)
    public List<StuForMark> selectForMark(Criteria example){
        return scoreMapper.selectForMark(example);
    }

    /**
     * 获取成绩列表数据总数
     * @return
     */
    @Transactional(readOnly = true)
    public int countForMark(Criteria example){
        return scoreMapper.countForMark(example);
    }
}